// CExclusions.cpp : implementation file
//

#include "stdafx.h"
#include "SQLInjectionISAPISettings.h"
#include "CExclusions.h"


// CExclusions dialog

IMPLEMENT_DYNAMIC(CExclusions, CPropertyPage)

CExclusions::CExclusions()
	: CPropertyPage(CExclusions::IDD)
	, enableExclusion(false)
{

}

CExclusions::~CExclusions()
{
}

BOOL CExclusions::IsValidFilename(CString &Filename)
{
	TCHAR invalidChars[] = { '"', '/', ':', '<', '>', '?', '\\', '|', '&', '[', ']', '{', '}' };
	CString invalidNames[] = { L"CLOCK$", L"AUX", L"CON", L"NUL", L"PRN", L"COM1", L"COM2",
		L"COM3", L"COM4", L"COM5", L"COM6", L"COM7", L"COM8", L"COM9", L"LPT1", L"LPT2", L"LPT3",
		L"LPT4", L"LPT5", L"LPT6", L"LPT7", L"LPT8", L"LPT9", L""  };
	// Any invalid character?
	for(int i=0; i<sizeof(invalidChars)/sizeof(TCHAR);i++)
	{
		if(Filename.Find(invalidChars[i]) >= 0)
			return FALSE;
	}
	//
	// * is only valid when starting the filename *.asp for instance
	//
	if(Filename.Left(2) == L"*.")
		return TRUE;
	if(Filename.Find('*') >= 0)
		return FALSE;
	for(int i=0;!invalidNames[i].IsEmpty();i++)
	{
		
		int size = invalidNames[i].GetLength();
		if(Filename.Left(size).CompareNoCase(invalidNames[i]) == 0)
		{
			// If the name is only a device name return false
			if(Filename.GetLength() == size)
				return FALSE;
			// if there is a . after the device name, also return false
			if(Filename.GetLength() > size && Filename[size] == '.')
				return FALSE;
		}
	}
	return TRUE;
}

void CExclusions::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT1, textFilename);
	DDX_Control(pDX, IDC_LISTFILES, listFilenames);
	DDX_Check(pDX, IDC_EXCLUSION, enableExclusion);
	if(listFilenames.GetItemCount() == 0)
	{
		for(int i=0;i<listString.GetCount();i++)
		{
			AddItem(listString[i]);
		}
	}

}


BEGIN_MESSAGE_MAP(CExclusions, CDialog)
	ON_BN_CLICKED(IDC_ADD, &CExclusions::OnBnClickedAdd)
	ON_BN_CLICKED(IDC_DELETE, &CExclusions::OnBnClickedDelete)
END_MESSAGE_MAP()

BOOL CExclusions::AddItem(CString &TextFile)
{
	BOOL result = false;
	if(!TextFile.IsEmpty())
	{
		LVFINDINFO lvf;
		lvf.flags = LVFI_STRING;
		lvf.psz = TextFile.GetBuffer();

		if(!IsValidFilename(TextFile))
		{
			this->MessageBox(L"Filename is invalid or contains invalid characters", L"Invalid Parameter");
			return false;

		}
		if(listFilenames.FindItem(&lvf) == -1)
		{
			listFilenames.InsertItem(listFilenames.GetItemCount(), lvf.psz);
			result = true;
		} else
		{
			this->MessageBox(L"File is already in the list", L"Invalid Parameter");
		}
		TextFile.ReleaseBuffer();
	} else
	{
		this->MessageBox(L"Please enter a filename first", L"Invalid Parameter");
	}
	return result;

}

// CExclusions message handlers

void CExclusions::OnBnClickedAdd()
{
	CString textFile;
	textFilename.GetWindowText(textFile);
	if(AddItem(textFile))
	{
		listString.Add(textFile);
	}
	textFilename.SetWindowTextW(L"");
	textFilename.SetFocus();

	// TODO: Add your control notification handler code here
}

void CExclusions::OnBnClickedDelete()
{
	int i = listFilenames.GetSelectionMark();
	if( i >= 0 )
	{
		listFilenames.DeleteItem(i);
		listString.RemoveAt(i);
	} else
	{
		this->MessageBox(L"Please select a filename in the list first", L"No Item Selected");
	}
	// TODO: Add your control notification handler code here
	textFilename.SetFocus();

}
